"""
Qt日志处理器 - 用于将日志重定向到GUI界面
"""
import logging
from PyQt5.QtCore import QObject, pyqtSignal


class QtLogHandler(QObject, logging.Handler):
    """Qt日志处理器，将日志发送到Qt信号"""
    # 定义信号，用于发送日志消息
    log_message = pyqtSignal(str, int)  # message, level
    
    def __init__(self):
        super().__init__()
        logging.Handler.__init__(self)
        
    def emit(self, record):
        """
        发出日志记录
        """
        try:
            msg = self.format(record)
            # 发送消息和级别
            self.log_message.emit(msg, record.levelno)
        except Exception:
            self.handleError(record)